package com.kangkang.teacher;

import java.util.*;

public class StudentSort {
    //类的功能要单一
    public static class Stu {
        private int id;
        private String name;
        private String grade;
        private String clsName;
        private List<Float> scores;

        public Stu(int id, String name, String grade, String cls) {
            this.id = id;
            this.name = name;
            this.grade = grade;
            this.clsName = cls;
            scores = new ArrayList<>();
        }

        public float getScoreSum() {
            float sum = 0;
            for (int i = 0; i < scores.size(); i++) {
                sum += scores.get(i);
            }
            return sum;
        }

        @Override
        public String toString() {
            return id + "," + name + "," + grade +
                    "," + clsName +
                    "," + getScoreSum();
        }

        public static Stu convert(String str) {
            String[] items = str.split("\t");
            if (items.length < 8) {
                return null;
            }
            int id = Integer.parseInt(items[0]);
            String name = items[1];
            String grade = items[2];
            String cls = items[3];
            Stu stu = new Stu(id, name, grade, cls);
            for (int i = 0; i < 4; i++) {
                stu.scores.add(Float.parseFloat(items[4 + i]));
            }
            return stu;
        }
    }

    public static List<String> readData() {
        Scanner sc = new Scanner(System.in);
        //int n = Integer.parseInt(sc.nextLine());//数据有多少条
        List<String> datas = new ArrayList<>();
        while (sc.nextLine()==null){
            datas.add(sc.nextLine());//nextLine:从控制台获取一行数据
        }
        return datas;
    }

    public static class Cls {
        private String clsName = "";
        private float scoreSum = 0.0f;
        private int count = 0;

        @Override
        public String toString() {
            return clsName +
                    "," + scoreSum +
                    "," + count +
                    "," + scoreSum / count;
        }
    }

    public static List<Cls> group(List<Stu> stus) {
        List<Cls> res = new ArrayList<>();
        for (Stu stu : stus) {
            String clsName = stu.clsName;
            boolean flag = false;
            for (Cls re : res) {
                if (clsName.equals(re.clsName)) {
                    flag = true;
                    re.count++;
                    re.scoreSum += stu.getScoreSum();
                    break;
                }
            }
            if (!flag) {
                Cls cls = new Cls();
                cls.clsName = stu.clsName;
                cls.count = 1;
                cls.scoreSum = stu.getScoreSum();
                res.add(cls);
            }

        }
        return res;
    }

    public static void main(String[] args) {
        List<String> datas = readData();
        List<Stu> stus = new ArrayList<>();
        for (String data : datas) {
            Stu s = Stu.convert(data);
            if (s != null) {
                stus.add(s);
            }
        }
        Collections.sort(stus, (o1, o2) -> {
            int diff = (int) (o1.getScoreSum() - o2.getScoreSum());
            if (diff == 0) {
                return o1.id - o2.id;
            }
            return -diff;
        });
        for (Stu stu : stus) {
            System.out.println(stu);
        }
        System.out.println("=================================");
        List<Cls> clses = group(stus);
        Collections.sort(clses,
                (o1, o2) -> o2.scoreSum / o2.count > o1.scoreSum / o1.count ? 1 : -1);
        for (Cls cls : clses) {
            System.out.println(cls);
        }
    }
}
